class code2 {
    public int minPathSum(int[][] grid) {
        int n = grid.length;
        int m = grid[0].length;

        int[][] dp = new int[n+1][m+1];
        //初始化
        for(int i=0;i<=m;i++) {
            dp[0][i] = Integer.MAX_VALUE;
        }
        for(int i=1;i<=n;i++) {
            dp[i][0] = Integer.MAX_VALUE;
        }

        for(int i=1;i<=n;i++) {
            for(int j=1;j<=m;j++) {
                //注意下标映射
                if(i==1 && j==1) {
                    dp[1][1] = grid[0][0];
                } else {
                    dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1]) + grid[i-1][j-1];
                }
            }
        }
        return dp[n][m];
    }
}